<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

  <script>
    /* 
      1. map 方法
      2. reduce 方法
      3. filter 方法
      4. find 方法
      5. findIndex 方法
      6. every 方法
      7. some 方法
     */

    const arr = [1, 3, 2, 6, 9, 7, 12];
    console.log('初始值：' + arr);

    // 1. map 方法实现
    Array.prototype.map = function (callback) {
      let arr = [];
      // 遍历数组
      for (let index = 0; index < this.length; index++) {
        // 调用回调函数
        const result = callback(this[index], index);
        arr.push(result);
      }

      return arr;
    }
    console.log(arr.map((item => item + 10)));

    // 2.reduce 方法实现
    Array.prototype.reduce = function (callback, initValue) {
      // 初始化返回的结果
      let result = initValue;
      for (let index = 0; index < this.length; index++) {
        const element = this[index];
        // 调用回调函数
        result = callback(result, element, index);
      }

      return result;
    }

    console.log(arr.reduce((count, item, index) =>
      count += (item % 2 != 0 ? 1 : 0), 0))

    // 3.filter 方法实现
    Array.prototype.filter = function (callback) {
      let arr = [];
      for (let index = 0; index < this.length; index++) {
        const element = this[index];
        if (callback(element, index)) {
          arr.push(element)
        };
      }

      return arr;
    }
    console.log(arr.filter(item => {
      if (item > 5) {
        return true
      }
    }))

    // 4. find 方法实现
    Array.prototype.find = function (callback) {
      for (let index = 0; index < this.length; index++) {
        const element = this[index];
        if (callback(element, index)) {
          return element
        }
      }

      return undefined
    }
    console.log(arr.find(item => {
      if (item > 5) {
        return true
      }
    }));

    // 5. findIndex 方法实现
    Array.prototype.findIndex = function (callback) {
      for (let index = 0; index < this.length; index++) {
        const element = this[index];

        if (callback(element, index)) {
          return index
        }
      }

      return -1;
    }
    console.log(arr.findIndex(item => {
      if (item > 6) {
        return true
      }
    }))

    // 6. every 方法实现
    Array.prototype.every = function (callback) {
      for (let index = 0; index < this.length; index++) {
        const element = this[index];
        if (!callback(element, index)) {
          return false
        }
      }

      return true
    }
    console.log(arr.every(item => {
      if (item > 1) {
        return true
      }
    }))

    // 7. some 方法实现
    Array.prototype.some = function (callback) {
      for (let index = 0; index < this.length; index++) {
        const element = this[index];
        if (callback(element, index)) {
          return true
        }
      }

      return false
    }
    console.log(arr.some( item => {
      if (item > 1) {
        return true
      }
    }))
  </script>
</body>

</html>